/**
 * 
 */
package com.example.android_is2.ListenerMap;

import java.util.Date;
import java.util.List;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;

import com.example.android_is2.MainActivity;
import com.example.android_is2.MapItemizedOverlay;
import com.example.android_is2.db.DBInfo;
import com.example.android_is2.db.DBTracker;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

/**
 * @author KAIN-PC
 * 
 */
public abstract class LocationListenerMap implements LocationListener {

  protected MapView mapView = null;
  private List<Overlay> overlays = null;
  private MapItemizedOverlay itemizedoverlay_my = null;
  private MapItemizedOverlay itemizedoverlay = null;
  private MapController mapcontroller = null;

  private DBInfo dbInfo = null;
  private DBTracker dbTracker = null;

  /**
   * @param mapView
   * @param overlays
   * @param itemizedoverlay_my
   */
  public LocationListenerMap(MapView mapView, List<Overlay> overlays,
      MapItemizedOverlay itemizedoverlay_my) {
    this.setMapView(mapView);
    this.setOverlays(overlays);
    this.setItemizedoverlay_my(itemizedoverlay_my);
    this.setMapcontroller(getMapView().getController());
  }
  
  public LocationListenerMap(MapView mapView, List<Overlay> overlays,
      MapItemizedOverlay itemizedOverlay_my, MapItemizedOverlay itemizedoverlay) {
    this(mapView,overlays,itemizedOverlay_my);
    this.setItemizedoverlay(itemizedoverlay);
  }


  /**
   * @param mapView
   * @param overlays
   * @param itemizedOverlay
   * @param itemizedoverlay2
   */
  public LocationListenerMap(MapView mapView, List<Overlay> overlays,
      MapItemizedOverlay itemizedoverlay_my, MapItemizedOverlay itemizedoverlay, DBInfo dbInfo) {
    this(mapView,overlays,itemizedoverlay_my,itemizedoverlay);
    this.setDbInfo(dbInfo);

  }
  
  /**
   * @param mapView
   * @param overlays
   * @param itemizedoverlay_my
   * @param itemizedoverlay
   * @param dbTracker
   */
  public LocationListenerMap(MapView mapView, List<Overlay> overlays,
      MapItemizedOverlay itemizedoverlay_my, MapItemizedOverlay itemizedoverlay, DBTracker dbTracker) {
    this(mapView,overlays,itemizedoverlay_my,itemizedoverlay);
    this.setDbTracker(dbTracker);
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.location.LocationListener#onLocationChanged(android.location.Location)
   */

  public MapView getMapView() {
    return mapView;
  }

  public void setMapView(MapView mapView) {
    this.mapView = mapView;
  }

  public List<Overlay> getOverlays() {
    return overlays;
  }

  public void setOverlays(List<Overlay> overlays) {
    this.overlays = overlays;
  }

  public MapItemizedOverlay getItemizedoverlay_my() {
    return itemizedoverlay_my;
  }

  public void setItemizedoverlay_my(MapItemizedOverlay itemizedoverlay_my) {
    this.itemizedoverlay_my = itemizedoverlay_my;
  }

  public MapItemizedOverlay getItemizedoverlay() {
    return itemizedoverlay;
  }

  public void setItemizedoverlay(MapItemizedOverlay itemizedoverlay) {
    this.itemizedoverlay = itemizedoverlay;
  }

  public MapController getMapcontroller() {
    return mapcontroller;
  }

  public void setMapcontroller(MapController mapcontroller) {
    this.mapcontroller = mapcontroller;
  }
  
  public DBInfo getDbInfo() {
    return dbInfo;
  }

  public void setDbInfo(DBInfo dbInfo) {
    this.dbInfo = dbInfo;
  }

  public DBTracker getDbTracker() {
    return dbTracker;
  }

  public void setDbTracker(DBTracker dbTracker) {
    this.dbTracker = dbTracker;
  }

  abstract public void onLocationChanged(Location location);

  /*
   * (non-Javadoc)
   * 
   * @see android.location.LocationListener#onProviderDisabled(java.lang.String)
   */
  public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
    Log.i(MainActivity.TAG + getClass(), "NetProvider_ENABLE_ON: NO");
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.location.LocationListener#onProviderEnabled(java.lang.String)
   */
  public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    Log.i(MainActivity.TAG + getClass(), "NetProvider_ENABLE_ON: YES");
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.location.LocationListener#onStatusChanged(java.lang.String, int,
   * android.os.Bundle)
   */
  public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
    if (status == LocationProvider.AVAILABLE) {
      Log.i(MainActivity.TAG + getClass(), "NetProvider_ENABLE_ON: " + status);
    } else {
      Log.i(MainActivity.TAG + getClass(), "NetProvider_ENABLE_ON: " + status);
    }
  }

  // FUNZIONE aggiornamento Location
  public static void updateLocationData(Location location) {
    Date timestamp = new Date(location.getTime());
    Log.i("LocationListenerMap", "TIME_STAMP: " + timestamp.toString());
    double latitude = location.getLatitude();
    Log.i("LocationListenerMap", "LATITUDE: " + String.valueOf(latitude));
    double longitude = location.getLongitude();
    Log.i("LocationListenerMap", "LONGITUDE: " + String.valueOf(longitude));
    if (location.hasAltitude()) {
      double altitude = location.getAltitude();
      Log.i("LocationListenerMap", "ALTITUDE: " + String.valueOf(altitude));
    }
    if (location.hasSpeed()) {
      float speed = location.getSpeed();
      Log.i("LocationListenerMap", "SPEED: " + String.valueOf(speed));
    }
  }

  // ------------------------------------

  // Funzione arrotondamento valore Double con numero di cifre Digit
  public static double getRound(double x, int digits) {
    double powerOfTen = Math.pow(10, digits);
    return (Math.round(x * powerOfTen) / powerOfTen);
  }
  // ------------------------------------

}
